#include <iostream>
#include <fstream>
#include "contacts.pb.h"

using namespace std;

void AddPeopleInfo(contacts2::PeopleInfo* people)
{
    cout<< "--------新增联系人--------"<<endl;
    cout<< "请输入姓名: ";
    string name;
    getline(cin, name);
    people->set_name(name);

    cout<< "请输入年龄: ";
    int age;
    cin >> age;
    people->set_age(age);
    cin.ignore(256, '\n'); // 清除输入缓冲区里面内容

    // 输入电话号码
    for(int i=0; ; i++)
    {
        cout<< "请输入联系人电话"<< i+1 <<"(只输入回车表示结束)"<<endl;
        string number;
        getline(cin, number);
        if(number.empty())
        {
            break;
        }
        contacts2::PeopleInfo_Phone* phone = people->add_phone();
        phone->set_number(number);
        cout<< "请输入电话类型(1.移动电话 2.座机): ";
        int type;
        cin>>type;
        cin.ignore(256, '\n');
        switch(type)
        {
            case 1:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout<<"输入有误"<<endl;
                break;
        }
    }
    
    contacts2::Address address;
    cout<< "家庭地址：";
    string home_address;
    getline(cin, home_address);
    address.set_home_address(home_address);
    cout<< "单位地址: ";
    string unit_address;
    getline(cin, unit_address);
    address.set_unit_address(unit_address);
    // Any
    // 将address对象转为any对象，存在people中,会返回一个bool值，判断是否转换成功
    people->mutable_data()->PackFrom(address);

    cout<< "添加其他联系方式(1.qq  2.wechat): ";
    int other_contact;
    cin>>other_contact;
    cin.ignore(256, '\n');
    if(1 == other_contact){
        cout<< "请输入qq：";
        string qq;
        getline(cin, qq);
        people->set_qq(qq);
    }
    else if(2 == other_contact){
        cout<< "请输入微信号：";
        string wechat;
        getline(cin, wechat);
        people->set_wechat(wechat);
    }
    else
    {
        cout<< "未添加其他联系方式"<<endl;
    }

    for(int i=0;;i++)
    {
        cout<< "请输入备注"<<i+1<<"标题（只回车表示结束）"<<endl;
        string remark_key;
        getline(cin, remark_key);
        if(remark_key.empty())
        {
            break;
        }
        cout<<"请输入备注"<<i+1<<"内容:"<<endl;
        string remark_value;
        getline(cin, remark_value);
        people->mutable_remark()->insert({remark_key, remark_value});

    }

    cout<< "-------添加成功-----------"<<endl;
}

int main()
{
    contacts2::Contacts contacts;
    // 读取通讯录已存在的联系人文件
    // 文件是二进制的
    fstream input("contacts.bin", ios::in | ios::binary);
    if(!input)
    {
        cout<< "contacts.bin is not exsit, create it! "<<endl;
    }
    else if(!contacts.ParseFromIstream(&input))
    {
        cerr<< "parse error!"<<endl;
        input.close();
        return -1;
    }


    // 向通讯录添加联系人
    AddPeopleInfo(contacts.add_contacts());
    // 将通讯录写入文件
    // ios::trunc 是覆盖文件写的意思
    fstream output("contacts.bin", ios::out | ios::trunc | ios::binary);
    if(!contacts.SerializeToOstream(&output))  // 将序列化的二进制数据写入output流
    {
        cerr << "write error!" << endl;
        input.close();
        output.close();
        return -1;
    }
    cout<< "write success!"<<endl;
    input.close();
    output.close();
    
    
    return 0;
}